//
//  ZtodoMissionView.m
//  WeClubs
//
//  Created by Zed Link on 11/4/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZTodoMissionView.h"
#import "ZTodoConfirmButton.h"
#import "ZClientInfoCenter.h"
#import "UIView+ZUtilities.h"
#import <UIImageView+WebCache.h>

@interface ZTodoMissionView ()

@property (weak, nonatomic) IBOutlet UIImageView *logoImgView;
@property (weak, nonatomic) IBOutlet UILabel *sponsorLabel;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UIButton *endDateBtn;
@property (weak, nonatomic) IBOutlet ZTodoConfirmButton *confirmBtn;
@property (weak, nonatomic) IBOutlet ZTodoConfirmButton *checkinBtn;

@end

@implementation ZTodoMissionView

- (void)zInitPrepare
{
    [super zInitPrepare];

    [self.logoImgView zCornerDeal:20];
    [self.endDateBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
    
    [self.confirmBtn setTitle:@"确认任务" forState:UIControlStateNormal];
    [self.confirmBtn setTitle:@"已确认任务" forState:UIControlStateSelected];
    
    [self.checkinBtn setTitle:@"完成任务" forState:UIControlStateNormal];
    [self.checkinBtn setTitle:@"已完成任务" forState:UIControlStateSelected];
}

- (void)updateCell
{
    self.sponsorLabel.text = self.model.sponsor.sponsorName;
    self.contentLabel.text = self.model.content;
    self.dateLabel.text = [[ZClientInfoCenter sharedInstance] stringByFormatt:@"MM月dd日" fromMilliSeconds:self.model.createDate];
    
    NSString *endDate = [[ZClientInfoCenter sharedInstance] stringByFormatt:@"MM月dd日 HH:mm" fromMilliSeconds:self.model.deadline];
    NSMutableAttributedString *endDateAttStr = [[NSMutableAttributedString alloc] initWithString:endDate attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSForegroundColorAttributeName : [UIColor zColorWithHex:0xeb4856]}];
    [endDateAttStr appendAttributedString:[[NSAttributedString alloc] initWithString:@" 截止" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12], NSForegroundColorAttributeName : [UIColor zColorWithHex:0xb2c1c6]}]];
    [self.endDateBtn setAttributedTitle:endDateAttStr forState:UIControlStateNormal];
    
    NSString *urlStr = self.model.sponsor.sponsorAvator;
    if (urlStr != nil) {
        [self.logoImgView sd_setImageWithURL:[NSURL URLWithString:urlStr]];
    }
}

#pragma mark - Control Action

- (IBAction)todoButtonAction:(ZTodoConfirmButton *)sender
{
    if (sender.selected == NO) {
        sender.selected = YES;
        sender.userInteractionEnabled = NO;
    }
}

#pragma mark - Getter & Setter

- (void)setModel:(ZTodoMissionModel *)model
{
    _model = model;
    
    [self updateCell];
}

@end
